package com.ruisoft.business.card.controller;

import java.io.IOException;
import java.util.List;

import com.ruisoft.base.controller.BaseController;
import com.ruisoft.business.card.service.EmployeeCardService;
import com.ruisoft.core.json.JSONMap;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 
 * @author LFC
 *
 */

@Controller
@RequestMapping("/employeeCard/")
public class EmployeeCardController extends BaseController {

	private static final Logger LOG = Logger.getLogger(EmployeeCardController.class);

	@Autowired
	private EmployeeCardService employeeCardService;

	@RequestMapping("query")
	public void query() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		if (queryData == null)
			queryData = getQueryData();
		try {
			List<JSONObject> result = employeeCardService.searchForPage(queryData);
			queryResponse(queryData, result);
		} catch (Exception e) {
			LOG.error("查询员工卡信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "执行查询员工卡信息操作发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("查询返回信息时发生错误", e);
			}
			queryData = null;
		}
	}
	
	
	@RequestMapping("queryCardInfoAll")
	public void queryCardInfoAll() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		if (queryData == null)
			queryData = getQueryData();
		try {
			List<JSONObject> result = employeeCardService.searchCardInfoForPage(queryData);
			queryResponse(queryData, result);
		} catch (Exception e) {
			LOG.error("查询员工卡信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "执行查询员工卡信息操作发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("查询返回信息时发生错误", e);
			}
			queryData = null;
		}
	}
	
	@RequestMapping("searchEmployeeCard/{userid}")
	public void searchEmployeeCard(@PathVariable String userid) {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try {
			JSONObject result = employeeCardService.searchEmployeeCard(userid);
			json.put("data", result);
			json.put("status", 1);
		} catch (Exception e) {
			LOG.error("查询库存分拣单：" + userid + "失败", e);
			json.put("status", "-2");
			json.put("msg", "执行查询员工卡信息：" + userid + "发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("返回信息时发生错误", e);
			}
		}
	}
	@RequestMapping("saveEmployeeCard")
	public void saveEmployee() {
		JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
		try {
			JSONObject reqData = getReqData();
			JSONObject result = new JSONObject();
			result = employeeCardService.saveEmployeeCard(reqData);
			json.put("result", result);
			json.put("status", "1");
		} catch (Exception e) {
			LOG.error("保存员工卡信息信息发生错误", e);
			json.put("status", "-2");
			json.put("msg", "保存员工卡信息信息发生错误");
		} finally {
			try {
				response(json);
			} catch (IOException e) {
				LOG.error("返回信息时发生错误", e);
			}
		}
	}
	@RequestMapping("deleteCardInfo/{userid}")
    public void deleteCardInfo(@PathVariable String userid) throws Exception{
        int status = employeeCardService.deleteCardInfo(userid);
        JSONMap<String, Object> json = new JSONMap<String, Object>(JSONMap.TYPE.OBJECT);
        json.put("status", status);
        response(json);
    }
}
